//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "SwaptionVolatilityDiscrete.h"
using namespace Cephei::QL::Termstructures::Volatility::Swaption;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/VolatilityTermStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityDiscrete::CSwaptionVolatilityDiscrete (boost::shared_ptr<QuantLib::SwaptionVolatilityDiscrete>& childNative, Object^ owner) : CVolatilityTermStructure(CSwaptionVolatilityDiscrete::typeid)
{
#ifdef HANDLE
	_phSwaptionVolatilityDiscrete = NULL;
#endif
	_ppSwaptionVolatilityDiscrete = &childNative;
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppSwaptionVolatilityDiscrete));
}
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityDiscrete::CSwaptionVolatilityDiscrete (QuantLib::SwaptionVolatilityDiscrete& childNative, Object^ owner) : CVolatilityTermStructure(CSwaptionVolatilityDiscrete::typeid)
{
#ifdef HANDLE
	_phSwaptionVolatilityDiscrete = NULL;
#endif
	_ppSwaptionVolatilityDiscrete = new boost::shared_ptr<QuantLib::SwaptionVolatilityDiscrete> (&childNative);
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppSwaptionVolatilityDiscrete));
    _SwaptionVolatilityDiscreteOwner = owner;
    _VolatilityTermStructureOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityDiscrete::CSwaptionVolatilityDiscrete (CSwaptionVolatilityDiscrete^ copy) : CVolatilityTermStructure(CSwaptionVolatilityDiscrete::typeid)
{
#ifdef HANDLE
	_phSwaptionVolatilityDiscrete = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSwaptionVolatilityDiscrete = new boost::shared_ptr<QuantLib::SwaptionVolatilityDiscrete> (copy->GetShared());
        _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppSwaptionVolatilityDiscrete));
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityDiscrete::CSwaptionVolatilityDiscrete (System::Type^ t) : CVolatilityTermStructure(CSwaptionVolatilityDiscrete::typeid)
{
#ifdef HANDLE
	_phSwaptionVolatilityDiscrete = NULL;
#endif
	if (!t->IsSubclassOf(CSwaptionVolatilityDiscrete::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityDiscrete::CSwaptionVolatilityDiscrete (QuantLib::Handle<QuantLib::SwaptionVolatilityDiscrete>& childNative, Object^ owner)  : CVolatilityTermStructure(CSwaptionVolatilityDiscrete::typeid)
{
	_phSwaptionVolatilityDiscrete = &childNative;
	_ppSwaptionVolatilityDiscrete = &static_cast<boost::shared_ptr<QuantLib::SwaptionVolatilityDiscrete>>(childNative.currentLink());
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppSwaptionVolatilityDiscrete));
    _SwaptionVolatilityDiscreteOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityDiscrete::CSwaptionVolatilityDiscrete (QuantLib::Handle<QuantLib::SwaptionVolatilityDiscrete> childNative)  : CVolatilityTermStructure(CSwaptionVolatilityDiscrete::typeid)
{
	_phSwaptionVolatilityDiscrete = &childNative;
	_ppSwaptionVolatilityDiscrete = &static_cast<boost::shared_ptr<QuantLib::SwaptionVolatilityDiscrete>>(childNative.currentLink());
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppSwaptionVolatilityDiscrete));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityDiscrete::CSwaptionVolatilityDiscrete (QuantLib::SwaptionVolatilityDiscrete childNative)  : CVolatilityTermStructure(CSwaptionVolatilityDiscrete::typeid)
{
#ifdef HANDLE
	_phSwaptionVolatilityDiscrete = NULL;
#endif
	_ppSwaptionVolatilityDiscrete = new boost::shared_ptr<QuantLib::SwaptionVolatilityDiscrete> (new QuantLib::SwaptionVolatilityDiscrete (childNative));
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppSwaptionVolatilityDiscrete));
}
#endif

Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityDiscrete::~CSwaptionVolatilityDiscrete ()
{
    if (_ppSwaptionVolatilityDiscrete != NULL)
    {
	    delete _ppSwaptionVolatilityDiscrete;
        _ppSwaptionVolatilityDiscrete = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityDiscrete::!CSwaptionVolatilityDiscrete ()
{
    if (_ppSwaptionVolatilityDiscrete != NULL)
    {
	    delete _ppSwaptionVolatilityDiscrete;
    }
}
QuantLib::SwaptionVolatilityDiscrete& Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityDiscrete::GetReference ()
{
    if (_ppSwaptionVolatilityDiscrete == NULL) throw gcnew NativeNullException ();
	return **_ppSwaptionVolatilityDiscrete;
}
boost::shared_ptr<QuantLib::SwaptionVolatilityDiscrete>& Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityDiscrete::GetShared ()
{
    if (_ppSwaptionVolatilityDiscrete == NULL) throw gcnew NativeNullException ();
	return *_ppSwaptionVolatilityDiscrete;
}
QuantLib::SwaptionVolatilityDiscrete* Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityDiscrete::GetPointer ()
{
    if (_ppSwaptionVolatilityDiscrete == NULL) throw gcnew NativeNullException ();
	return &**_ppSwaptionVolatilityDiscrete;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SwaptionVolatilityDiscrete>& Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityDiscrete::GetHandle ()
{
	if (_phSwaptionVolatilityDiscrete == NULL)
	{
		_phSwaptionVolatilityDiscrete = new Handle<QuantLib::SwaptionVolatilityDiscrete> (*_ppSwaptionVolatilityDiscrete);
	}
	return *_phSwaptionVolatilityDiscrete;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityDiscrete::HasNative () 
{
	return (_ppSwaptionVolatilityDiscrete != NULL);
}

Cephei::IVector<DateTime>^ Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityDiscrete::OptionDates::get ()
{
    try
    {
    	std::vector<QuantLib::Date>& _rv = (std::vector<QuantLib::Date>&)(*_ppSwaptionVolatilityDiscrete)->optionDates ( );   
        Cephei::IVector<DateTime>^ _nrv = gcnew CoVector<DateTime> (gcnew CDateTimeVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Cephei::QL::Times::IPeriod^>^ Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityDiscrete::OptionTenors::get ()
{
    try
    {
    	std::vector<QuantLib::Period>& _rv = (std::vector<QuantLib::Period>&)(*_ppSwaptionVolatilityDiscrete)->optionTenors ( );   
        CoVector<Cephei::QL::Times::IPeriod^>^ _nrv = gcnew CoVector<Cephei::QL::Times::IPeriod^>(gcnew CPeriodVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityDiscrete::OptionTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppSwaptionVolatilityDiscrete)->optionTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::ISwaptionVolatilityDiscrete^ Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityDiscrete::PerformCalculations::get ()
{
    try
    {
    	(*_ppSwaptionVolatilityDiscrete)->performCalculations ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityDiscrete::SwapLengths::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppSwaptionVolatilityDiscrete)->swapLengths ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Cephei::QL::Times::IPeriod^>^ Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityDiscrete::SwapTenors::get ()
{
    try
    {
    	std::vector<QuantLib::Period>& _rv = (std::vector<QuantLib::Period>&)(*_ppSwaptionVolatilityDiscrete)->swapTenors ( );   
        CoVector<Cephei::QL::Times::IPeriod^>^ _nrv = gcnew CoVector<Cephei::QL::Times::IPeriod^>(gcnew CPeriodVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::ISwaptionVolatilityDiscrete^ Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityDiscrete::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppSwaptionVolatilityDiscrete)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

